package com.xiashitech.agent.instrumentation.apachehttpclient.common.v2v3;

import io.opentelemetry.javaagent.shaded.io.opentelemetry.context.propagation.TextMapGetter;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;

import java.util.Arrays;
import java.util.stream.Collectors;

public enum HttpClientHeaderGetter implements TextMapGetter<HttpMethod> {
  INSTANCE;

  @Override
  public Iterable<String> keys(HttpMethod carrier) {
    return Arrays.stream(carrier.getResponseHeaders()).map(header -> header.getValue()).collect(Collectors.toList());
  }

  @Override
  public String get(HttpMethod carrier, String key) {
    Header header = carrier.getResponseHeader(key);
    String headValue = null;
    if (header != null) {
      headValue = header.getValue();
    }
    return headValue;
  }
}
